package javafx;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.BasicStroke;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Draw extends JFrame{
    public static void main(String[] args) {
        //在主函数中，实例化Draw类的对象，调用初始化界面的方法
        Draw draw = new Draw();
        draw.Ondraw();
    }

    private Paint[] array = new Paint[100000];//定义存储图形的数组
    private int x1, y1, x2, y2;

    //重写父类的重绘方法
    public void paint (Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);// 设置画笔抗锯齿
        //把存储在数组中的图形数据取出来，重新画一次
        for (int i=0;i<array.length;i++) {
            Paint p = array[i];//获取数组中指定下标位置的图形对象
            if(p!=null) {
                x1=p.getX1();
                x2=p.getX2();
                y1=p.getY1();
                y2=p.getY2();

                g.setColor(p.getColor());
                if(p.getName().equals("直线")) {
                    g.drawLine(x1, y1, x2, y2);
                } else if(p.getName().equals("矩形")) {
                    g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2),Math.abs(y1-y2));
                } else if(p.getName().equals("圆")) {
                    g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2),Math.abs(y1-y2));
                } else if(p.getName().equals("文字")) {
                    g.drawString("这是文字效果", x1, y1);
                } else if(p.getName().equals("铅笔")||p.getName().equals("刷子")||p.getName().equals("橡皮")) {
                    g2d.setStroke(new BasicStroke(p.getWidth()));
                    g2d.drawLine(x1, y1, x2, y2);
                } else if(p.getName().equals("喷枪")) {
                    g.drawLine(x1,y1,x2,y2);
                } else if(p.getName().equals("长方体")) {
                    int a,b,c,d;
                    a=Math.min(x1, x2);
                    b=Math.max(x1, x2);
                    c=Math.min(y1, y2);
                    d=Math.max(y1, y2);
                    int m=(int)((b-a)*Math.cos(Math.PI/4)*Math.sin(Math.PI/4));
                    int n=(int)((b-a)*Math.cos(Math.PI/4)*Math.sin(Math.PI/4));
                    g.setColor(new Color(100, 200, 100));
                    g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2),Math.abs(y1-y2));
                    g.setColor(Color.green);
                    g.fillPolygon(new int[] {a, a+m, b+m,b},new int[] {c,c-n,c-n,c},4);
                    g.setColor(Color.black);
                    g.fillPolygon(new int[] {b, b, b+m,b+m},new int[] {c,d,d-n,c-n},4);
                }
                else
                    break;

            }
        }
    }

    public void Ondraw() {

        //在初始化界面的方法中，实例化JFrame窗体容器组件类的对象
        //JFrame frame = new JFrame();
        //设置窗体容器组件对象的属性值：标题、大小、显示位置、关闭操作、可见。
        this.setTitle("画板");
        this.setSize(700,700);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);

        //实例化FlowLayout流式布局类的对象，设置对齐方式
        FlowLayout fl = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(fl);

        //在实例化DrawListener类的对象时将获取的画笔对象传递过去
        DrawListener dl = new DrawListener(this,array);

        String[] typeArray = {"直线","矩形","圆","文字","铅笔","刷子","橡皮","喷枪","长方体"};
        for(int i=0;i<typeArray.length;i++) {
            JButton button = new JButton(typeArray[i]);
            button.setPreferredSize(new Dimension(80,30));
            this.add(button);
            button.addActionListener(dl);//添加动作监听方法
        }

        Color[] colorArray = {Color.red,Color.green,Color.blue};
        for(int i=0;i<colorArray.length;i++) {
            JButton button = new JButton();
            button.setBackground(colorArray[i]);
            button.setPreferredSize(new Dimension(30,30));
            this.add(button);
            button.addActionListener(dl);//添加动作监听方法
        }

        this.setVisible(true);

        //获取窗体上的画笔对象
        Graphics g = this.getGraphics();

        //给窗体添加鼠标事件监听方法，指定事件的处理类的对象dl;
        dl.setG(g);//设置方法将画笔g传到DrawListener
        this.addMouseListener(dl);
        this.addMouseMotionListener(dl);
    }
}